package com.yao.code;

import com.yao.tool.ListNode;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author yaoquanwei
 * @date 2021/8/8 11:11
 */
public class Code210808 {
    /**
     * 周赛题 5838.检查字符串是否为数组前缀
     */
    public static boolean isPrefixString(String s, String[] words) {
        int p = 0;
        int i = 0;
        while (p < s.length() && i < words.length) {
            String tem = words[i];
            if (p + tem.length() > s.length() || !tem.equals(s.substring(p, p + tem.length()))) {
                return false;
            }
            i++;
            p += tem.length();
        }
        if (p < s.length()) {
            return false;
        }
        return true;
    }

    /**
     * 每日两题 1.两数之和
     */
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                res[0] = i;
                res[1] = map.get(target - nums[i]);
                return res;
            }
            map.put(nums[i], i);
        }
        return res;
    }

    /**
     * 每日两题 2.两数相加
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(-1);
        ListNode pre = res;
        int t = 0;
        while (l1 != null || l2 != null || t != 0) {
            if (l1 != null) {
                t += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                t += l2.val;
                l2 = l2.next;
            }
            pre.next = new ListNode(t % 10);
            pre = pre.next;
            t /= 10;
        }
        return res.next;
    }

    public static void main(String[] args) {
        String[] word = {"ii", "love", "you", "mm"};
        System.out.println(isPrefixString("i", word));
    }

    /**
     * 剑指offer 1. 用两个栈实现队列
     */
    class CQueue {
        Stack<Integer> stack1;
        Stack<Integer> stack2;

        public CQueue() {
            stack1 = new Stack<Integer>();
            stack2 = new Stack<Integer>();
        }

        public void appendTail(int value) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }
            stack1.push(value);
        }

        public int deleteHead() {
            if (stack1.isEmpty() && stack2.isEmpty()) {
                return -1;
            }
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }

    /**
     * 剑指offer 2. 包含min函数的栈
     */
    class MinStack {

        Stack<Integer> stack;
        Stack<Integer> minStack;

        public MinStack() {
            stack = new Stack<Integer>();
            minStack = new Stack<Integer>();
        }

        public void push(int x) {
            stack.push(x);
            if (minStack.isEmpty()) {
                minStack.push(x);
            } else {
                minStack.push(x < minStack.peek() ? x : minStack.peek());
            }
        }

        public void pop() {
            stack.pop();
            minStack.pop();
        }

        public int top() {
            return stack.peek();
        }

        public int min() {
            return minStack.peek();
        }
    }
}
